package org.niu.str;

import java.util.Locale;

public class LicenseKeyFormatting {
    public String licenseKeyFormatting(String s, int k) {
        StringBuilder stringBuilder = new StringBuilder();
        StringBuilder ans = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            if(s.charAt(i)!='-'){
                stringBuilder.append(s.charAt(i));
            }
        }
        int len = stringBuilder.length();
        int mod = len % k;
        if(mod != 0){
            ans.append(stringBuilder.substring(0, mod-1));
        }
        for(int i = mod;i < stringBuilder.length();i++){
            if(i % k == 0 && i != 0){
                ans.append('-');
            }
            ans.append(stringBuilder.charAt(i));
        }
        return ans.toString().toUpperCase();
    }
}
